package com.jmx.tomcat.servlet;

import com.jmx.tomcat.http.HttpRequest;
import com.jmx.tomcat.http.HttpResponse;
import com.jmx.tomcat.servlet.api.GenericServlet;
import com.jmx.tomcat.utils.WebUtil;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author: jmx
 * @date: 2023-08-11 19:32
 * @version: 1.0
 */
public class CalcServlet extends GenericServlet {
    @Override
    public void doGet(HttpRequest request, HttpResponse response) {
        doPost(request,response);
    }

    @Override
    public void doPost(HttpRequest request, HttpResponse response) {
        int n1 = WebUtil.strToInt(request.getParameter("n1"));
        int n2 = WebUtil.strToInt(request.getParameter("n2"));
        int result = n1+n2;
        OutputStream outputStream = response.getOutputStream();
        String respBody = "<h1>"+n1+"+"+n2+"="+result+"<h1>";
        try {
            outputStream.write(respBody.getBytes(StandardCharsets.UTF_8));
            //System.out.println(Thread.currentThread().getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
